﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using weikebuluo.DAL;
using weikebuluo.Common;
using weikebuluo.Model;

namespace weikebuluo.web.wechat.guaguaka
{
    public partial class guaguaka : System.Web.UI.Page
    {
        h_ggklog_dal ggklogdal = new h_ggklog_dal();
        h_ggklog_model ggklogmodel = new h_ggklog_model();

        h_ggk_dal ggkdal = new h_ggk_dal();
        h_ggk_model ggkmodel = new h_ggk_model();

        h_ggks_dal ggksdal = new h_ggks_dal();
        h_ggks_model ggksmodel = new h_ggks_model();

        v_vip_dal vipdal = new v_vip_dal();
        protected v_vip_model vipmodel = new v_vip_model();

        protected string award = "1";
        protected string award2 = "0";
        protected string forward = "1";
        protected string times = "0";
        protected string logo = "styles/logo.png";
        protected string footer = "styles/footer.png";
        protected void Page_Load(object sender, EventArgs e)
        {
            Random rand = new Random();
            

            if (!string.IsNullOrEmpty(Request.QueryString["hid"])&&!string.IsNullOrEmpty(Request.QueryString["uid"]) && !string.IsNullOrEmpty(Request.QueryString["wxid"]))
            {
                DataTable dtggk = ggkdal.GetList("user_id=" + Request.QueryString["uid"] + "and ggks_id=" + Request.QueryString["hid"]).Tables[0];
                DataTable dtggklog = ggklogdal.GetList("user_id=" + Request.QueryString["uid"] + " and ggklog_wxid='" + Request.QueryString["wxid"] + "' and ggks_id=" + Request.QueryString["hid"] + " and DateDiff(dd,ggklog_time,getdate())=0").Tables[0];

                //DataTable dtvip = vipdal.GetList("openid='" + Request.QueryString["wxid"] + "'").Tables[0];
                //if (dtvip.Rows.Count == 0)
                //{
                //    Response.Write("<script Language=\"Javascript\">alert(\"刮卡前先绑定vip卡！\")");
                //    Response.Write("</script>");
                //    Response.Write("<script Language=\"Javascript\">location.href=\"/web/wechat/vip/index.aspx?uid="+Request.QueryString["uid"]+"&wxid="+Request.QueryString["wxid"]+"\";");
                //    Response.Write("</script>");
                //    Response.End();
                //}

                if (dtggklog.Rows.Count > 0)
                {
                    award = dtggklog.Rows[0]["ggklog_award"].ToString();
                    if (dtggklog.Rows.Count > 1)
                    {
                        award2 = dtggklog.Rows[1]["ggklog_award"].ToString();
                    }
                    forward = dtggklog.Rows[0]["ggklog_stutas"].ToString();
                    times = dtggklog.Rows.Count.ToString();
                }
                else {
                    int r = rand.Next(1, 999);//本次幸运数字
                    int sum = 0;
                    for (int i = 0; i < dtggk.Rows.Count; i++) { //得到中奖奖项
                        sum += int.Parse(dtggk.Rows[i]["ggk_chance"].ToString());
                        if (r <= sum) {
                            int count = ggklogdal.GetRecordCount("user_id=" + Request.QueryString["uid"] + " and ggklog_award='" + dtggk.Rows[i]["ggk_award"].ToString() + "' and ggks_id=" + Request.QueryString["hid"]);
                            if (count < int.Parse(dtggk.Rows[i]["ggk_num"].ToString()))//判断是否超过数量，不超过则中奖，否则不中奖；
                            {
                                award = dtggk.Rows[i]["ggk_award"].ToString();
                            }
                            else {
                                award = "1";
                            }
                            break;
                        }
                    }

                }
                //图片logo 和活动时间
                DataTable dtggks = ggksdal.GetList("user_id=" + Request.QueryString["uid"] + " and ggks_id=" + Request.QueryString["hid"]).Tables[0];
                if (dtggks.Rows.Count > 0)
                {
                    logo = dtggks.Rows[0]["ggks_url1"].ToString();
                    footer = dtggks.Rows[0]["ggks_url2"].ToString();

                    if (DateTime.Parse(dtggks.Rows[0]["ggks_time1"].ToString()) > DateTime.Now)
                    {
                        Response.Write("活动未开始！");
                        Response.End();
                    }
                    else if (DateTime.Parse(dtggks.Rows[0]["ggks_time2"].ToString()) < DateTime.Now)
                    {
                        Response.Write("活动结束！");
                        Response.End();
                    }
                }
                else {
                    Response.Write("没有活动！");
                    Response.End();
                }
            }
            else if (string.IsNullOrEmpty(Request.QueryString["hid"]) || string.IsNullOrEmpty(Request.QueryString["uid"]) || string.IsNullOrEmpty(Request.QueryString["wxid"]))
            {
                Response.Write("请用微信登录！");
                Response.End();
            }
            

            switch (Request.QueryString["action"]) {
                case "isor":
                    gua();
                    break;
                case "isorno":
                    gua2();
                    break;
                case "second":
                    share();
                    break;
                //case "duijiang":
                //    duijiang();
                //    break;
            }
        }

        //刮1
        private void gua() {

            DataTable dtggklog = ggklogdal.GetList("user_id=" + Request.QueryString["uid"] + " and ggklog_wxid='" + Request.QueryString["wxid"] + "'and ggks_id=" + Request.QueryString["hid"] + " and  DateDiff(dd,ggklog_time,getdate())=0").Tables[0];
            

            if (dtggklog.Rows.Count > 0)
            {
            }
            else
            {
                ggklogmodel.ggklog_award = Request.Form["first"];
                ggklogmodel.ggklog_stutas = 1;
                ggklogmodel.ggklog_wxid = Request.QueryString["wxid"];
                ggklogmodel.user_id = int.Parse(Request.QueryString["uid"].Trim().ToString());
                ggklogmodel.ggks_id = int.Parse(Request.QueryString["hid"].Trim().ToString());
                ggklogmodel.ggklog_time = DateTime.Now;
                ggklogdal.Add(ggklogmodel);
                if (Request.Form["first"] != "1")
                {
                    Response.Write("200");
                }
                else {
                    Response.Write("0");
                }
                Response.End();
            }
        }
        //刮2
        private void gua2()
        {
            DataTable dtggklog = ggklogdal.GetList("user_id=" + Request.QueryString["uid"] + " and ggklog_wxid='" + Request.QueryString["wxid"] + "' and ggks_id=" + Request.QueryString["hid"] + " and DateDiff(dd,ggklog_time,getdate())=0").Tables[0];
            if (dtggklog.Rows.Count == 1)
            {
                ggklogmodel.ggklog_award = Request.Form["second"];
                ggklogmodel.ggklog_stutas = 1;
                ggklogmodel.ggklog_wxid = Request.QueryString["wxid"];
                ggklogmodel.user_id = int.Parse(Request.QueryString["uid"].Trim().ToString());
                ggklogmodel.ggks_id = int.Parse(Request.QueryString["hid"].Trim().ToString());
                ggklogmodel.ggklog_time = DateTime.Now;
                ggklogdal.Add(ggklogmodel);
            }
            Response.End();

        }
        //分享
        private void share() {
            DataTable dtggklog = ggklogdal.GetList("user_id=" + Request.QueryString["uid"] + " and ggklog_wxid='" + Request.QueryString["wxid"] + "' and ggks_id=" + Request.QueryString["hid"] + " and  DateDiff(dd,ggklog_time,getdate())=0").Tables[0];
            ggklogmodel = ggklogdal.GetModel(int.Parse(dtggklog.Rows[0]["ggklog_id"].ToString()));
            ggklogmodel.ggklog_stutas = 2;//状态为2代表被分享过
            ggklogdal.Update(ggklogmodel);
            Response.End();
        }
        ////兑奖
        //private void duijiang() {
        //    DataTable dtggklog = ggklogdal.GetList("user_id=" + Request.QueryString["uid"] + " and ggklog_wxid='" + Request.QueryString["wxid"] + "'and ggks_id=" + Request.QueryString["hid"]).Tables[0];
        //    ggklogmodel = ggklogdal.GetModel(int.Parse(dtggklog.Rows[0]["ggklog_id"].ToString()));
        //    ggklogmodel.ggklog_stutas = 3;
        //    ggklogdal.Update(ggklogmodel);
        //    Response.Write("200");
        //    Response.End();
        //}

    }
}